using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using System.Xml;

namespace XSLTListitemWebPart
{
    public class RenderTemplateEditorPart : EditorPart
    {
        protected DropDownList ddlRenderTemplate;
        protected CheckBox cbRenderTemplateOverride;

        public RenderTemplateEditorPart()
        {
            ID = "RenderTemplateEditorPart";
        }

        protected override void CreateChildControls()
        {
            XSLTListitemWebPartFeature webpart = (XSLTListitemWebPartFeature) this.WebPartToEdit;

            Table sectionTableRenderTemplate;
            Control sectionRenderTemplate = EditorPartHelper.createConfigureSectionControls(this, "Expand Category: Render Template", "Collapse Category: Render Template", "Render Template", "RenderTemplate", "", out sectionTableRenderTemplate);
            Controls.Add(sectionRenderTemplate);

            ddlRenderTemplate = new DropDownList();
            EditorPartHelper.addToolPaneControl(sectionTableRenderTemplate, "Template Name", ddlRenderTemplate, false);

            ddlRenderTemplate.Items.Clear();
            ddlRenderTemplate.Items.Add( new ListItem("(from list)",""));
            try
            {
                XmlDocument xslFile = new XmlDocument();
                xslFile.LoadXml(SPContext.Current.Site.RootWeb.GetFileAsString(webpart.XSLTLink));
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xslFile.NameTable);
                nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
                XmlNodeList list = xslFile.DocumentElement.SelectNodes("/xsl:stylesheet/xsl:template", nsmgr);

                foreach (XmlNode templatenode in list)
                {
                    try
                    {
                        string templatename = templatenode.Attributes.GetNamedItem("name").Value;
                        if (ddlRenderTemplate.Items.FindByValue(templatename) == null)
                        {
                            ddlRenderTemplate.Items.Add(new ListItem(templatename, templatename));
                        }
                    }
                    catch
                    { }
                }
            }
            catch
            {
            }

            base.CreateChildControls();
        }

        public override void SyncChanges()
        {
            EnsureChildControls();            
            XSLTListitemWebPartFeature webpart = (XSLTListitemWebPartFeature)this.WebPartToEdit;
            ListItem item = ddlRenderTemplate.Items.FindByValue(webpart.RenderTemplateName);
            if (item != null)
            {
                int itemidx = ddlRenderTemplate.Items.IndexOf(item);
                ddlRenderTemplate.SelectedIndex = itemidx;
            }
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            XSLTListitemWebPartFeature webpart = (XSLTListitemWebPartFeature)this.WebPartToEdit;
            webpart.RenderTemplateName = ddlRenderTemplate.SelectedValue;
            return true;
        }

    }
}
